package com.iafme.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iafme.dto.req.NewsReqDTO;
import com.iafme.dto.res.NewsResDTO;
import com.iafme.mapper.NewsMapper;
import com.iafme.model.News;
import com.iafme.service.NewsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author summer
 * @version 1.0
 * @date 2025/2/23 16:36
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {
    @Override
    public Page<NewsResDTO> lists(NewsReqDTO newsReqDTO) {
        Page<News> page=new Page<>(newsReqDTO.getPageNum(), newsReqDTO.getPageSize());
        LambdaQueryWrapper<News> like = Wrappers.lambdaQuery(News.class).like(StrUtil.isNotEmpty(newsReqDTO.getName()), News::getName, newsReqDTO.getName()).orderByDesc(News::getCreateTime);
        return Convert.convert(new TypeReference<Page<NewsResDTO>>() {
        },this.page(page, like));
    }
}
